import { ref, resolveComponent, nextTick } from 'vue'
import { DictDirection } from '@/store/modules/dicts'
import { businessList } from '@/api/myMission/businessHandling/processApplication'
import { resetForm } from '@/utils'

export default function ({ $form }) {
  const reset = () => {
      resetForm($form.value.formComponent)
    },
    validate = () => new Promise((resolve, reject) => {
      $form.value.formComponent.validate((valid) => {
        if (valid) {
          resolve($form.value.form)
        } else {
          reject(new Error('表单验证不通过'))
        }
      })
    })
  /* 容器组件 */
  const dialogVisible = ref(false)
  const config = {
    query: {
      fields: [
        {
          label: '任务名称',
          prop: 'taskName',
        },
        {
          label: '流程发起人',
          prop: 'taskName',
        },
      ],
      queryParams: {
        pageSize: 10,
        pageNum: 1,
        processKey: 'process_1',
      },
    },
    btns: [
      {
        title: '新增',
        // directives: [['hasPermi', ['订单'], 'arg']],
        onClick() {
          dialogVisible.value = true
        },
        type: 'primary',
      },
    ],
    table: {
      columns: [
      // { type: 'index', label: '序号', width: '55' },
        { label: '客户简称', prop: 'customerName' },
        { label: '客户全称', prop: 'wholeName' },
        { label: '创建时间', prop: 'updateTime' },
        { label: '流程状态', prop: 'status' },
        { label: '客户等级', prop: 'grade' },
        { label: '类型',
          prop: 'type',
          minWidth: '100',
          align: 'center',
          autoFormatter: {
            dict: DictDirection.客户类型,
            rowKey: 'type',
          }, 
        },
        {
          label: '操作',
          className: 'custormFixedCol',
          showOverflowTooltip: false,
          fixed: 'right',
          minWidth: '120',
          render(h, params) {
            return [
              h(resolveComponent('ElButton'),
                {
                  text: true,
                  type: 'primary',
                  onClick: () => {
                    console.log(params)
                  },
                },
                {
                  default: () => '重新申请',
                }),
              h(resolveComponent('ElButton'),
                {
                  text: true,
                  type: 'primary',
                  onClick: () => {
                    dialogVisible.value = true
                    nextTick(() => {
                      $form.value.form = JSON.parse(JSON.stringify(params.row))
                    })
                  },
                },
                {
                  default: () => '详情',
                }),
            ]
          },
        },
      ],
      // event: {
      //   onRowClick(e) {
      //     console.log(e)
      //   },
      // },
    },
    api: businessList,
    customKey: 'myMission/businessHandling/NewCustomer', 
  }
  /* 弹窗 */
  const title = ref('新增需求')
  const cancel = () => {
    reset()
    dialogVisible.value = false
  }
  return {
    config,
    dialogVisible,
    title,
    reset,
    validate,
    cancel,
  } 
}